package com.ruoyi.web.controller.market.special;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.SpecialCostomItemInfo;
import com.ruoyi.db.domain.SpecialOperatorLog;
import com.ruoyi.special.constant.CondiConstant;
import com.ruoyi.special.model.vo.CondiInfoVO;
import com.ruoyi.special.service.ISpecialCostomItemInfoService;
import com.ruoyi.special.service.ISpecialOperatorLogService;
import com.ruoyi.sys.service.IAddressInfoService;
import com.ruoyi.web.wapper.SpecialCostomItemInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 机构推送子任务Controller
 *
 * @author ruoyi
 * @date 2022-11-11
 */
@Controller
@RequestMapping("/special/itemInfo")
public class SpecialCostomItemInfoController extends BaseController {
    private String prefix = "special/itemInfo";

    @Autowired
    private ISpecialCostomItemInfoService specialCostomItemInfoService;
    @Autowired
    private IAddressInfoService addressInfoService;
    @Autowired
    ISpecialOperatorLogService specialOperatorLogService;

    @GetMapping("/{costomId}")
    public String itemInfoPost(@PathVariable("costomId") String costomId, ModelMap mmap) {
        mmap.put("costomId", costomId);
        return prefix + "/itemInfo";
    }

    /**
     * 查询机构推送子任务列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SpecialCostomItemInfo specialCostomItemInfo) {
        startPage();
        List<Map<String, Object>> mapList = specialCostomItemInfoService.selectSpecialCostomItemInfoListMap(specialCostomItemInfo);
        mapList = new SpecialCostomItemInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }

    /**
     * 删除特殊渠道信息
     */
    @RequiresPermissions("special:itemInfo:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(specialCostomItemInfoService.deleteSpecialCostomItemInfoByIds(ids));
    }

    /**
     * 任务查询
     *
     * @return
     */
    @RequiresPermissions("special:itemInfo:query")
    @GetMapping("/query")
    public String query() {
        return prefix + "/query";
    }

    /**
     * 任务查询
     */
    @RequiresPermissions("special:itemInfo:query")
    @PostMapping("/queryList")
    @ResponseBody
    public TableDataInfo queryList(SpecialCostomItemInfo specialCostomItemInfo) {
        startPage();
        List<Map<String, Object>> mapList = specialCostomItemInfoService.selectSpecialCostomItemInfoQueryListMap(specialCostomItemInfo);
        mapList = new SpecialCostomItemInfoWapper(mapList).wrap();
        return getDataTable(mapList);
    }


    /**
     * 新增机构推送子任务
     */
    @RequiresPermissions("special:itemInfo:add")
    @GetMapping("/add/{costomId}")
    public String add(@PathVariable("costomId") Long costomId, ModelMap mmap) {
        mmap.put("costomId", costomId);
        mmap.put("multiInfoList", CondiConstant.dataList);

        return prefix + "/add";
    }

    /**
     * 新增保存机构推送子任务
     */
    @RequiresPermissions("special:itemInfo:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SpecialCostomItemInfo specialCostomItemInfo) {
        return toAjax(specialCostomItemInfoService.insertSpecialCostomItemInfo(specialCostomItemInfo));
    }

    /**
     * 修改机构推送子任务
     */
    @RequiresPermissions("special:itemInfo:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        SpecialCostomItemInfo specialCostomItemInfo = specialCostomItemInfoService.selectSpecialCostomItemInfoById(id);
        mmap.put("specialCostomItemInfo", specialCostomItemInfo);

        List<CondiInfoVO> checkedDataList = new ArrayList<>();

        CondiConstant.getCheckdNotPushWeekDataList(checkedDataList, specialCostomItemInfo.getNotPushDayWeek());
        CondiConstant.getCheckdLoanAmountDataList(checkedDataList, specialCostomItemInfo.getLoanAmount());
        CondiConstant.getCheckdZhiMaDataList(checkedDataList, specialCostomItemInfo.getZhima());
        CondiConstant.getCheckdBaiTiaoDataList(checkedDataList, specialCostomItemInfo.getBaitao());
        CondiConstant.getCheckdHuaBeiDataList(checkedDataList, specialCostomItemInfo.getHuabei());
        CondiConstant.getCheckdXueLiDataList(checkedDataList, specialCostomItemInfo.getXueli());
        CondiConstant.getCheckdYuqiDataList(checkedDataList, specialCostomItemInfo.getYuqi());
        CondiConstant.getCheckdHouseDataList(checkedDataList, specialCostomItemInfo.getHouse());
        CondiConstant.getCheckdCarDataList(checkedDataList, specialCostomItemInfo.getCar());
        CondiConstant.getCheckdGjjDataList(checkedDataList, specialCostomItemInfo.getGjj());
        CondiConstant.getCheckdSocialInsuranceDataList(checkedDataList, specialCostomItemInfo.getSocialInsurance());
        CondiConstant.getCheckdInsuranceDataList(checkedDataList, specialCostomItemInfo.getInsurance());
        CondiConstant.getCheckdEnterpriseOwnerDataList(checkedDataList, specialCostomItemInfo.getEnterpriseOwner());
        CondiConstant.getCheckdOrFilterDataList(checkedDataList, specialCostomItemInfo.getOrFilter());


        mmap.put("multiInfoList", checkedDataList);

        return prefix + "/edit";
    }

    /**
     * 修改机构推送子任务
     */
    @RequiresPermissions("special:itemInfo:add")
    @GetMapping("/copy/{id}")
    public String copy(@PathVariable("id") Long id, ModelMap mmap) {
        SpecialCostomItemInfo specialCostomItemInfo = specialCostomItemInfoService.selectSpecialCostomItemInfoById(id);
        mmap.put("specialCostomItemInfo", specialCostomItemInfo);

        List<CondiInfoVO> checkedDataList = new ArrayList<>();

        CondiConstant.getCheckdNotPushWeekDataList(checkedDataList, specialCostomItemInfo.getNotPushDayWeek());
        CondiConstant.getCheckdLoanAmountDataList(checkedDataList, specialCostomItemInfo.getLoanAmount());
        CondiConstant.getCheckdZhiMaDataList(checkedDataList, specialCostomItemInfo.getZhima());
        CondiConstant.getCheckdBaiTiaoDataList(checkedDataList, specialCostomItemInfo.getBaitao());
        CondiConstant.getCheckdHuaBeiDataList(checkedDataList, specialCostomItemInfo.getHuabei());
        CondiConstant.getCheckdXueLiDataList(checkedDataList, specialCostomItemInfo.getXueli());
        CondiConstant.getCheckdYuqiDataList(checkedDataList, specialCostomItemInfo.getYuqi());
        CondiConstant.getCheckdHouseDataList(checkedDataList, specialCostomItemInfo.getHouse());
        CondiConstant.getCheckdCarDataList(checkedDataList, specialCostomItemInfo.getCar());
        CondiConstant.getCheckdGjjDataList(checkedDataList, specialCostomItemInfo.getGjj());
        CondiConstant.getCheckdSocialInsuranceDataList(checkedDataList, specialCostomItemInfo.getSocialInsurance());
        CondiConstant.getCheckdInsuranceDataList(checkedDataList, specialCostomItemInfo.getInsurance());
        CondiConstant.getCheckdEnterpriseOwnerDataList(checkedDataList, specialCostomItemInfo.getEnterpriseOwner());
        CondiConstant.getCheckdOrFilterDataList(checkedDataList, specialCostomItemInfo.getOrFilter());

        mmap.put("multiInfoList", checkedDataList);

        return prefix + "/copy";
    }

    /**
     * 修改保存机构推送子任务
     */
    @RequiresPermissions("special:itemInfo:edit")
    @Log(title = "机构推送子任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SpecialCostomItemInfo specialCostomItemInfo) {
        if (StrUtil.isEmpty(specialCostomItemInfo.getCityName())) {
            return error("推送城市不能为空");
        }
        return toAjax(specialCostomItemInfoService.updateSpecialCostomItemInfo(specialCostomItemInfo));
    }

    /**
     *
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap) {
        SpecialCostomItemInfo specialCostomItemInfo = specialCostomItemInfoService.selectSpecialCostomItemInfoById(id);
        mmap.put("specialCostomItemInfo", specialCostomItemInfo);

        List<CondiInfoVO> checkedDataList = new ArrayList<>();

        CondiConstant.getCheckdNotPushWeekDataList(checkedDataList, specialCostomItemInfo.getNotPushDayWeek());
        CondiConstant.getCheckdLoanAmountDataList(checkedDataList, specialCostomItemInfo.getLoanAmount());
        CondiConstant.getCheckdZhiMaDataList(checkedDataList, specialCostomItemInfo.getZhima());
        CondiConstant.getCheckdBaiTiaoDataList(checkedDataList, specialCostomItemInfo.getBaitao());
        CondiConstant.getCheckdHuaBeiDataList(checkedDataList, specialCostomItemInfo.getHuabei());
        CondiConstant.getCheckdXueLiDataList(checkedDataList, specialCostomItemInfo.getXueli());
        CondiConstant.getCheckdYuqiDataList(checkedDataList, specialCostomItemInfo.getYuqi());
        CondiConstant.getCheckdHouseDataList(checkedDataList, specialCostomItemInfo.getHouse());
        CondiConstant.getCheckdCarDataList(checkedDataList, specialCostomItemInfo.getCar());
        CondiConstant.getCheckdGjjDataList(checkedDataList, specialCostomItemInfo.getGjj());
        CondiConstant.getCheckdSocialInsuranceDataList(checkedDataList, specialCostomItemInfo.getSocialInsurance());
        CondiConstant.getCheckdInsuranceDataList(checkedDataList, specialCostomItemInfo.getInsurance());
        CondiConstant.getCheckdEnterpriseOwnerDataList(checkedDataList, specialCostomItemInfo.getEnterpriseOwner());
        CondiConstant.getCheckdOrFilterDataList(checkedDataList, specialCostomItemInfo.getOrFilter());

        mmap.put("multiInfoList", checkedDataList);

        return prefix + "/view";
    }


    @RequiresPermissions("special:itemInfo:change")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SpecialCostomItemInfo specialCostomItemInfo) {
        SpecialOperatorLog specialOperatorLog = new SpecialOperatorLog();
        specialOperatorLog.setOperatorName(getSysUser().getUserName());
        specialOperatorLog.setOperationType(Long.valueOf(specialCostomItemInfo.getStatus()));
        specialOperatorLog.setOperatorId(getSysUser().getUserId());
        specialOperatorLog.setInfoId(specialCostomItemInfo.getInfoId());
        specialOperatorLog.setTiskId(specialCostomItemInfo.getId());
        specialOperatorLog.setType(1);
        specialOperatorLogService.insertSpecialOperatorLog(specialOperatorLog);
        return toAjax(specialCostomItemInfoService.updateSpecialCostomItemInfo(specialCostomItemInfo));
    }

    /**
     * 加载地址管理树列表
     */
    @GetMapping("/inCity")
    @ResponseBody
    public List<Ztree> inCity(@RequestParam("itemInfoId") String itemInfoId) {
        SpecialCostomItemInfo specialCostomItemInfo =
                specialCostomItemInfoService.selectSpecialCostomItemInfoById(Long.valueOf(itemInfoId));

        List<String> inCityList = JSON.parseArray(specialCostomItemInfo.getCityName(), String.class);
        List<Ztree> ztrees = addressInfoService.selectAddressInfoTreeOfChoose(inCityList);
        return ztrees;
    }

    /**
     * 加载地址管理树列表
     */
    @GetMapping("/outCity")
    @ResponseBody
    public List<Ztree> outCity(@RequestParam("itemInfoId") String itemInfoId) {
        SpecialCostomItemInfo specialCostomItemInfo =
                specialCostomItemInfoService.selectSpecialCostomItemInfoById(Long.valueOf(itemInfoId));

        List<String> inCityList = JSON.parseArray(specialCostomItemInfo.getOutCityArray(), String.class);
        List<Ztree> ztrees = addressInfoService.selectAddressInfoTreeOfChoose(inCityList);
        return ztrees;
    }

    /**
     * 任务查询
     *
     * @return
     */
    @GetMapping("/count/{itemId}")
    public String count(ModelMap modelMap, @PathVariable("itemId") String itemId) {
        modelMap.put("itemId", itemId);
        return prefix + "/count";
    }

    /**
     * 查询机构推送子任务列表
     */
    @PostMapping("/countList")
    @ResponseBody
    public TableDataInfo countList(
            @RequestParam("itemInfoId") String itemInfoId,
            @RequestParam(value = "dateInfo", required = false) String dateInfo
    ) {

        String startDate = "";
        String endDate = "";
        if (StrUtil.isNotEmpty(dateInfo)) {
            String[] split = dateInfo.split("~");
            startDate = StrUtil.trim(split[0]);
            endDate = StrUtil.trim(split[1]);
        }

        startPage();
        List<Map<String, Object>> mapList = specialCostomItemInfoService.selectCountListMap(itemInfoId, startDate, endDate);
        return getDataTable(mapList);
    }

    /**
     * 新增机构推送子任务
     */
    @RequiresPermissions("special:itemInfo:batchEdit")
    @GetMapping("/batchEdit/{costomId}")
    public String batchEdit(@PathVariable("costomId") Long costomId, ModelMap mmap) {
        mmap.put("costomId", costomId);
        return prefix + "/batchEdit";
    }


    @RequiresPermissions("special:itemInfo:batchEdit")
    @Log(title = "任务信息-批量修改", businessType = BusinessType.UPDATE)
    @PostMapping("/batchEditSave")
    @ResponseBody
    public AjaxResult batchEditSave(@RequestParam("infoId") String infoId,
                                    @RequestParam("starttime") String starttime,
                                    @RequestParam("endtime") String endtime) {

        return toAjax(specialCostomItemInfoService.updateByIds(infoId,starttime,endtime));
    }

}
